<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;


use AuctionBack\Logic\ActivityLogic;
use AuctionBack\Logic\ClcwUserLogic;
use AuctionCore\Common\AjaxPage;
use AuctionCore\Common\Image;
use AuctionCore\Common\Util;

class ActivityController extends BaseController
{
    private $activityLogic;
    private $clcwUserLogic;
    private $empId;

    public function __construct()
    {
        parent::__construct();
        $this->activityLogic = new ActivityLogic();
        $this->clcwUserLogic = new ClcwUserLogic();
        $this->empId = $this->user_info['emp_id'];
    }

    public function index()
    {
        $this->display();
    }


    public function activityList()
    {
        $page = I("get.p", 1);
        $size = 10;
        $params = [];
        $params['id'] = I('get.aid/d');
        $params['name'] = I('get.name', '', 'trim');
        $params['emp_user'] = I('get.emp_user', '', 'trim');
        $params['start'] = I('get.start_time', '');
        $params['end'] = I('get.end_time', '');
        $params['status'] = I('get.status/d', 0);
        $result = $this->activityLogic->getActivityList($page, $size, $params);
        $Page = new AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show();   // 分页显示输出
        //模板赋值
        $this->assign("p", $page);
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->display();
    }

    public function addActivity()
    {
        $this->display();
    }

    /**
     *  添加活动
     */
    public function addActivityData()
    {
        $activityName = I('post.activity_name', '', 'trim');
        if (empty($activityName)) {
            $this->helpJsonResult(400, '抱歉，请输入活动名称');
        }
        $activityExpertUids = I('post.activity_expert_uids', '', 'trim');
        if (empty($activityExpertUids)) {
            $this->helpJsonResult(400, '抱歉，请输入活动专家');
        }
        $activityExpertIntros = I('post.activity_expert_intros', '', 'trim');
        if (empty($activityExpertIntros)) {
            $this->helpJsonResult(400, '抱歉，请输入专家简介');
        }
        $activityExperts = array_combine($activityExpertUids, $activityExpertIntros);
        $activityPcImage = I('post.activity_pc_image', '');
        if (empty($activityPcImage)) {
            $this->helpJsonResult(400, '抱歉，请添加桌面端图片');
        }
        $activityAppImage = I('post.activity_app_image', '');
        if (empty($activityAppImage)) {
            $this->helpJsonResult(400, '抱歉，请添加移动端图片');
        }
        $activityIntro = I('post.activity_intro', '');
        if (empty($activityIntro)) {
            $this->helpJsonResult(400, '抱歉，请输入活动简介');
        }
        $activityRule = I('post.activity_rule', '');
        if (empty($activityRule)) {
            $this->helpJsonResult(400, '抱歉，请输入活动简介');
        }
        $data['activity_name'] = $activityName;
        $data['activity_experts'] = $activityExperts;
        $data['activity_pc_image'] = $activityPcImage;
        $data['activity_app_image'] = $activityAppImage;
        $data['activity_intro'] = $activityIntro;
        $data['activity_rule'] = $activityRule;
        $data['emp_id'] = $this->empId;
        $res = $this->activityLogic->addActivity($data);
        if (!$res) {
            $this->helpJsonResult(400, '添加失败');
        }
        $this->helpJsonResult(200, '添加成功');
    }

    public function editActivity()
    {
        $id = I('get.id/d', 0);
        if ($id < 0) {
            $this->helpJsonResult(400, '页面错误');
        }
        $activity = $this->activityLogic->getActivityById($id);
        $activityExpertList = $this->activityLogic->getActivityExpert($id);
        foreach ($activityExpertList as $k => $v) {
            $expertInfo = $this->clcwUserLogic->getExpertUserInfoByUid($v['expert_id']);
            $activityExpertList[$k]['nick_name'] = $expertInfo['nick_name'] ? $expertInfo['nick_name'] : showHideMobile($expertInfo['mobile']);
        }
        $activityExpertCount = count($activityExpertList);
        $this->assign('activity', $activity);
        $this->assign('activityId', $activity['activity_id']);
        $this->assign('activityExpertList', $activityExpertList);
        $this->assign('activityExpertCount', 4 - $activityExpertCount);
        $this->display();
    }

    /**
     * 保存活动
     */
    public function saveActivityData()
    {
        $activityId = I('post.activity_id/d', 0);
        if ($activityId < 1) {
            $this->helpJsonResult(400, '抱歉，活动找不到了');
        }
        $activityName = I('post.activity_name', '', 'trim');
        if (empty($activityName)) {
            $this->helpJsonResult(400, '抱歉，请输入活动名称');
        }
        $activityExpertUids = I('post.activity_expert_uids', '', 'trim');

        if (empty($activityExpertUids)) {
            $this->helpJsonResult(400, '抱歉，请输入活动专家');
        }
        $activityExpertIntros = I('post.activity_expert_intros', '', 'trim');
        if (empty($activityExpertIntros)) {
            $this->helpJsonResult(400, '抱歉，请输入专家简介');
        }
        $activityExperts = array_combine($activityExpertUids, $activityExpertIntros);
        $activityPcImage = I('post.activity_pc_image', '');
        if (empty($activityPcImage)) {
            $this->helpJsonResult(400, '抱歉，请添加桌面端图片');
        }
        $activityAppImage = I('post.activity_app_image', '');
        if (empty($activityAppImage)) {
            $this->helpJsonResult(400, '抱歉，请添加移动端图片');
        }
        $activityIntro = I('post.activity_intro', '');
        if (empty($activityIntro)) {
            $this->helpJsonResult(400, '抱歉，请输入活动简介');
        }
        $activityRule = I('post.activity_rule', '');
        if (empty($activityRule)) {
            $this->helpJsonResult(400, '抱歉，请输入活动简介');
        }
        $data['activity_id'] = $activityId;
        $data['activity_name'] = $activityName;
        $data['activity_experts'] = $activityExperts;
        $data['activity_pc_image'] = $activityPcImage;
        $data['activity_app_image'] = $activityAppImage;
        $data['activity_intro'] = $activityIntro;
        $data['activity_rule'] = $activityRule;
        $res = $this->activityLogic->updateActivity($data);
        if (!$res) {
            $this->helpJsonResult(500, '编辑失败');
        }
        $this->helpJsonResult(200, '编辑成功');
    }

    /**
     * 设置活动状态(1、上线 2、下线)
     */
    public function setActivityStatus()
    {
        try {
            $activityId = I('post.activityId/d', 0);
            $activityStatus = I('post.activityStatus/d', 0);
            $activity = [];
            $activity['activity_id'] = $activityId;
            $activity['status'] = $activityStatus;
            $res = $this->activityLogic->updateActivity($activity);
            if (!$res) {
                $this->helpJsonResult('500', '操作失败');
            }
            $this->helpJsonResult('200', '操作成功');
        } catch (AuctionException $e) {
            $this->helpJsonResult($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 检查活动是否存在
     */
    public function checkActivity()
    {
        $activityId = I('post.a_id/d', 0);
        $activityInfo = D('Activity', 'Logic')->getActivityById($activityId);
        if (empty($activityInfo)) {
            $this->helpJsonResult(400, '抱歉，未查询到活动');
        }
        $this->helpJsonResult(200, '', $activityInfo);
    }

    /**
     * 上传图片
     */
    public function uploadPic()
    {
        try {
            $ret = [];
            foreach ($_FILES as $key => $file) {
                $tmpFile = $file['tmp_name'];
                if ($file['size'] > 2 * 1024 * 1024) {
                    echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '500', 'msg' => '图片尺寸太大']);
                    exit;
                }
                $object = $tmpFile . '.' . get_extension($file['name']);
                $ret[] = Image::upload_file_by_file(OSS_BUCKET, $object, $tmpFile, null, '', 'activity');
            }
            echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '200', 'msg' => '', 'data' => $ret]);
            exit;
        } catch (\Exception $e) {
            echo Util::auto_jsonp_or_json($_REQUEST, ['code' => '5', 'msg' => $e->getMessage()]);
            exit;
        }
    }


    public function saveActivityName()
    {
        $activityId = I('post.aid/d', 0);
        $activityName = I('post.activity_name', '', 'trim');
        if (empty($activityName)) {
            $this->helpJsonResult(400, '抱歉，请输入活动名称');
        }
        $data['activity_id'] = $activityId;
        $data['activity_name'] = $activityName;
        $res = $this->activityLogic->updateActivity($data);
        if (!$res) {
            $this->helpJsonResult(400, '保存失败');
        }
        $this->helpJsonResult(200, '保存成功');
    }

    public function saveActivityTopicId()
    {
        $activityId = I('post.aid/d', 0);
        $topicId = I('post.topicId/d', 0);
        if ($topicId < 1) {
            $this->helpJsonResult(400, '抱歉，请输入专题');
        }
        $topic = D('Topic', 'Logic')->getTopicById($topicId);
        if (empty($topic)) {
            $this->helpJsonResult(400, '抱歉，专题不正确');
        }
        $data['activity_id'] = $activityId;
        $data['topic_id'] = $topicId;
        $res = $this->activityLogic->updateActivity($data);
        if (!$res) {
            $this->helpJsonResult(400, '保存失败');
        }
        $this->helpJsonResult(200, '保存成功');
    }

    public function saveActivityIntro()
    {
        $activityId = I('post.aid/d', 0);
        $intro = I('post.intro', '');
        if (empty($intro)) {
            $this->helpJsonResult(400, '抱歉，请输入活动简介');
        }
        $data['activity_id'] = $activityId;
        $data['activity_intro'] = $intro;
        $res = $this->activityLogic->updateActivity($data);
        if (!$res) {
            $this->helpJsonResult(400, '保存失败');
        }
        $this->helpJsonResult(200, '保存成功');
    }

    public function saveExpertId()
    {
        $activityId = I('post.aid/d', 0);
        $expertIds = I('post.expert_ids', '');
        $data['activity_id'] = $activityId;
        $data['expert_id'] = $expertIds;
        $res = $this->activityLogic->updateActivity($data);
        if (!$res) {
            $this->helpJsonResult(400, '保存失败');
        }
        $this->helpJsonResult(200, '保存成功');
    }

    public function saveArticleId()
    {
        $activityId = I('post.aid/d', 0);
        $articleIds = I('post.article_ids', '');
        $data['activity_id'] = $activityId;
        $data['article_id'] = $articleIds;
        $res = $this->activityLogic->updateActivity($data);
        if (!$res) {
            $this->helpJsonResult(400, '保存失败');
        }
        $this->helpJsonResult(200, '保存成功');
    }

    public function saveActivityRules()
    {
        $activityId = I('post.aid/d', 0);
        $rules = I('post.rules', '');
        if (empty($rules)) {
            $this->helpJsonResult(400, '抱歉，请输入活动规则');
        }
        $data['activity_id'] = $activityId;
        $data['activity_rule'] = $rules;
        $res = $this->activityLogic->updateActivity($data);
        if (!$res) {
            $this->helpJsonResult(400, '保存失败');
        }
        $this->helpJsonResult(200, '保存成功');
    }
}